package flc.ast.activity;

import static com.hjq.permissions.XXPermissions.REQUEST_CODE;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelPictureAdapter;
import flc.ast.databinding.ActivitySelPicBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelPicActivity extends BaseAc<ActivitySelPicBinding> {

    public static int type;
    private SelPictureAdapter pictureAdapter;
    private List<String> listSel = new ArrayList<>();
    private int oldPosition;
    private String selPath = "";

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_pic;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivSelPicBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvEnter.setOnClickListener(this);

        mDataBinding.rvPicList.setLayoutManager(new GridLayoutManager(mContext, 3));
        pictureAdapter = new SelPictureAdapter();
        mDataBinding.rvPicList.setAdapter(pictureAdapter);
        pictureAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvEnter:
                if (listSel == null && selPath == null) {
                    ToastUtils.showShort(getString(R.string.please_sel_pic));
                    return;
                }
                if (type == 0) {
                    for (String path : listSel) {
                        String filePath = FileUtil.generateFilePath(Content.myAlbum + "/" + AlbumActivity.sFilePath, "." + FileUtils.getFileExtension(path));
                        FileUtils.copy(path, filePath);
                    }
                    finish();
                } else if (type == 1) {
                    PicFilterActivity.imgPath = selPath;
                    startActivity(PicFilterActivity.class);
                } else if (type == 2) {
                    PicAdjustActivity.imgPath = selPath;
                    startActivity(PicAdjustActivity.class);
                } else if (type == 3) {
                    PicCutActivity.imgPath = selPath;
                    startActivity(PicCutActivity.class);
                } else if (type == 4) {
                    PicStickerActivity.imgPath = selPath;
                    startActivity(PicStickerActivity.class);
                } else if (type == 5) {
                    PicTextActivity.imgPath = selPath;
                    startActivity(PicTextActivity.class);
                } else if (type == 6) {
                    PicBackgroundActivity.imgPath = selPath;
                    startActivity(PicBackgroundActivity.class);
                } else if (type == 7) {
                    PicDoodleActivity.imgPath = selPath;
                    startActivity(PicDoodleActivity.class);
                } else if (type == 8) {
                    if (listSel.size() < 2) {
                        ToastUtils.showShort(getString(R.string.please_sel_multiple));
                        return;
                    }
                    PuzzleActivity.listPath = listSel;
                    startActivity(PuzzleActivity.class);
                }
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (position == 0) {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivity(takePictureIntent);
            return;
        }
        if (type == 0 || type == 8) {
            if (pictureAdapter.getItem(position).isChecked()) {
                pictureAdapter.getItem(position).setChecked(false);
                listSel.remove(pictureAdapter.getItem(position).getPath());
            } else {
                pictureAdapter.getItem(position).setChecked(true);
                listSel.add(pictureAdapter.getItem(position).getPath());
            }
            pictureAdapter.notifyItemChanged(position);
        } else {
            pictureAdapter.getItem(oldPosition).setChecked(false);
            pictureAdapter.notifyItemChanged(oldPosition);
            oldPosition = position;
            pictureAdapter.getItem(position).setChecked(true);
            pictureAdapter.notifyItemChanged(position);
            selPath = pictureAdapter.getItem(position).getPath();
        }
    }

    @Override
    protected void initData() {
        getPermission();
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO).reqPermissionDesc(getString(R.string.get_picture_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                getData();
            }
        }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                selectMediaEntities.add(0, new SelectMediaEntity());
                pictureAdapter.setList(selectMediaEntities);
            }
        });
    }

}